package org.hawk.gwt.ppc;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.util.Arrays;

import org.hawk.gwt.ppc.javacc.ParseException;

import junit.framework.TestCase;

public class GWTModuleTestCase extends TestCase {
	public void testInheritedModules() 
		throws MalformedURLException, ParseException {
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertTrue(Arrays.equals(module.getInheritedModules(), 
			new String[]{"com.google.gwt.user.User",
			"org.hawk.gwt.ppc.mock.mock"}));
		module = module("org.hawk.gwt.ppc.mock.mock2");
		assertEquals(0,module.getInheritedModules().length);
	}

	public void testClientPackages() 
		throws MalformedURLException, ParseException {
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertTrue(Arrays.equals(module.getClientPackages(), 
			new String[]{"org.hawk.gwt.ppc.mock.client",
				"org.hawk.gwt.ppc.mock.server",
				"org.hawk.gwt.ppc.mock.common"}));
	}

	public void testEntryPoints() 
		throws MalformedURLException, ParseException {
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertEquals(0,module.getEntryPoints().length);
		module = module("org.hawk.gwt.ppc.mock.mock2");
		assertTrue(Arrays.equals(module.getEntryPoints(), 
			new String[]{"org.hawk.gwt.ppc.mock.client.EntryPointImpl1",
				"org.hawk.gwt.ppc.mock.client.EntryPointImpl2",
				"org.hawk.gwt.ppc.mock.client.EntryPointImpl3"}));
		module = module("org.hawk.gwt.ppc.mock.mock");
		assertTrue(Arrays.equals(module.getEntryPoints(), 
			new String[]{"org.hawk.gwt.ppc.mock.client.EntryPointImpl"}));
	}

	public void testAddInheritance() 
		throws ParseException, IOException {
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertTrue(Arrays.equals(module.getInheritedModules(), 
			new String[]{"com.google.gwt.user.User",
			"org.hawk.gwt.ppc.mock.mock"}));
		module.addInheritance("org.hawk.gwt.ppc.mock.mock2");
		assertTrue(Arrays.equals(module.getInheritedModules(), 
				new String[]{"com.google.gwt.user.User",
				"org.hawk.gwt.ppc.mock.mock",
				"org.hawk.gwt.ppc.mock.mock2"}));
	}

	public void testName() throws MalformedURLException, ParseException {
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertEquals("org.hawk.gwt.ppc.mock.mock1", module.getName());
	}

	public void testToStream() throws IOException, ParseException {
		StringWriter check = new StringWriter();
		GWTModule module = module("org.hawk.gwt.ppc.mock.mock1");
		assertTrue(Arrays.equals(module.getInheritedModules(), 
			new String[]{"com.google.gwt.user.User",
			"org.hawk.gwt.ppc.mock.mock"}));
		module.addInheritance("org.hawk.gwt.ppc.mock.mock2");
		Reader reader = new InputStreamReader(module.toStream());
		char[] buf = new char[1024];
		int len = 0;
		while((len=reader.read(buf))>0){
			check.write(buf,0,len);
		}
		assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
			"<module rename-to=\"mock1\">"+
			"<inherits name=\"com.google.gwt.user.User\"/>"+
			"<inherits name=\"org.hawk.gwt.ppc.mock.mock\"/>"+
			"<source path=\"client\"/>"+
			"<source path=\"server\"/>"+
			"<source path=\"common\"/>"+
			"<inherits name=\"org.hawk.gwt.ppc.mock.mock2\"/>"+
		"</module>", check.toString().replaceAll("[\n\t\r\b]+", ""));
	}

	private GWTModule module(String name) throws MalformedURLException, ParseException {
		GWTModule module = new GWTModule(name,new File("./src/",name.replace('.','/')+".gwt.xml").toURI().toURL());
		return module;
	}
}
